package cn.swust.designpatternlearningplatform.controller;

import cn.swust.designpatternlearningplatform.common.AnnotationUtils.Authentication;
import cn.swust.designpatternlearningplatform.common.Result.PageInfo;
import cn.swust.designpatternlearningplatform.common.Result.R;
import cn.swust.designpatternlearningplatform.common.utils.AuthUserContext;
import cn.swust.designpatternlearningplatform.common.utils.JwtUtils;
import cn.swust.designpatternlearningplatform.domain.bo.StudentTopicTypeBo;
import cn.swust.designpatternlearningplatform.domain.bo.TokenInfoBo;
import cn.swust.designpatternlearningplatform.domain.bo.TopicWrongWithKnowledgePointBo;
import cn.swust.designpatternlearningplatform.domain.dto.TopicWrongDto;
import cn.swust.designpatternlearningplatform.domain.dto.TopicWrongPointLinkDto;
import cn.swust.designpatternlearningplatform.domain.enums.TopicUpdateTypeEnum;
import cn.swust.designpatternlearningplatform.service.TopicWrongService;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/topic")
public class TopicWrongController {
    /**
     * 错题
     *
     * @author Tang
     * @date 2023/5/2 22:06
     **/
    @Resource
    private TopicWrongService topicWrongService;

    /**
     * 根据知识点id来获取相应的错题
     *
     * @param pageInfo
     * @param pointId
     * @return cn.swust.designpatternlearningplatform.common.Result.R<cn.swust.designpatternlearningplatform.common.Result.PageInfo < cn.swust.designpatternlearningplatform.domain.dto.TopicWrongDto>>
     * @author Tang
     * @date 2023/4/20 17:39
     **/
    @Authentication
    @PostMapping("/getErr")
    public R<PageInfo<TopicWrongDto>> getWrongTopicByKnowledgePointId(@RequestBody PageInfo<TopicWrongDto> pageInfo,
                                                                      @RequestParam(value = "pointId", defaultValue = "1") Integer pointId) {
        //学生的学号
        TokenInfoBo tokenInfoBo = AuthUserContext.get();
        if (tokenInfoBo == null || tokenInfoBo.getNumber().isEmpty()) {
            return R.error("登录信息有误");
        }
        //知识点检查
        if (pointId == null) {
            return R.error("知识点不能为空");
        }
        String studentNumber = tokenInfoBo.getNumber();
        //查询与知识点point相关联且为学生错题的题目
        TopicWrongWithKnowledgePointBo topicWrongWithKnowledgePointBo = new TopicWrongWithKnowledgePointBo();
        StudentTopicTypeBo studentTopicTypeBo = getStudentTopicTypeBo(pageInfo, pointId, studentNumber, topicWrongWithKnowledgePointBo);
        //查询错题个数
        Integer totalTopicWrong = topicWrongService.totalTopicWrongIdUnderPoint(topicWrongWithKnowledgePointBo, studentTopicTypeBo);
        //查询与知识点关联的错题题目
        List<TopicWrongDto> wrongTopicInKnowledgePoint = topicWrongService.
                getWrongTopicInKnowledgePoint(pageInfo, topicWrongWithKnowledgePointBo, studentTopicTypeBo);
        //封装分页对象
        pageInfo.setData(wrongTopicInKnowledgePoint);
        pageInfo.setTotal(totalTopicWrong);
        return R.success(pageInfo);
    }

    @NotNull
    private static StudentTopicTypeBo getStudentTopicTypeBo(PageInfo<TopicWrongDto> pageInfo, Integer pointId, String studentNumber, TopicWrongWithKnowledgePointBo topicWrongWithKnowledgePointBo) {
        topicWrongWithKnowledgePointBo.setPointId(pointId);
        topicWrongWithKnowledgePointBo.setStudentNumber(studentNumber);
        StudentTopicTypeBo studentTopicTypeBo = new StudentTopicTypeBo();
        studentTopicTypeBo.setStudentNumber(studentNumber);
        studentTopicTypeBo.setCurrent(pageInfo.getCurrentPage());
        studentTopicTypeBo.setSize(pageInfo.getPageSize());
        studentTopicTypeBo.setTopicType(TopicUpdateTypeEnum.WRONG.getTypeName());
        return studentTopicTypeBo;
    }

    /**
     * 获取错题列表
     *
     * @param token
     * @param pageInfo
     * @return cn.swust.designpatternlearningplatform.common.Result.R<cn.swust.designpatternlearningplatform.common.Result.PageInfo < cn.swust.designpatternlearningplatform.domain.dto.TopicWrongPointLinkDto>>
     * @author Tang
     * @date 2023/7/20 13:31
     **/
    @Authentication
    @GetMapping("/getErrList")
    public R<PageInfo<TopicWrongPointLinkDto>> getWrongTopicByKnowledgePointId(@RequestHeader("token") String token,
                                                                               PageInfo<TopicWrongPointLinkDto> pageInfo) {
        if (pageInfo.getCurrentPage() <= 0) {
            return R.error("页码错误");
        }
        //学生的学号
        String studentNumber = JwtUtils.getClaims(token).get(JwtUtils.NUMBER, String.class);
        List<TopicWrongPointLinkDto> topicPointLinkDto = topicWrongService
                .getTopicPointLinkDto(studentNumber, pageInfo.getCurrentPage(), pageInfo.getPageSize());
        pageInfo.setData(topicPointLinkDto);
        return R.success(pageInfo);
    }
}
